package com.gzdh.dishrecognition.ui.fragment.sysset;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.text.InputType;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.SysConstConfig;
import com.gzdh.dishrecognition.event.CartEvent;
import com.gzdh.dishrecognition.ui.dialog.InputDialog;
import com.gzdh.dishrecognition.ui.view.OptionItemView;

import org.greenrobot.eventbus.EventBus;

public class DinnerSetFragment extends Fragment {
    private Context mContext;

    private Activity activity;

    public DinnerSetFragment(Activity activity) {
        this.activity = activity;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        mContext = inflater.getContext();
        View view = getViews();

        return view;
    }

    private View getViews() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout mainLayout = new LinearLayout(mContext);
        mainLayout.setLayoutParams(layoutParams);
        mainLayout.setBackgroundColor(Color.WHITE);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.addView(getTitleView());   //标题初始化
        mainLayout.addView(getBody());        //主题内容
        return mainLayout;
    }


    private View getTitleView() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout titleLinear = new LinearLayout(mContext);
        titleLinear.setLayoutParams(layoutParams);
        titleLinear.setBackgroundColor(Color.WHITE);
        titleLinear.setOrientation(LinearLayout.VERTICAL);

        RelativeLayout titleRv = new RelativeLayout(mContext);
        titleRv.setLayoutParams(layoutParams);
        titleLinear.addView(titleRv);

        //-------------------------标题
        TextView titleTv = new TextView(mContext);
        titleTv.setText(" 点 餐 模 式 基 础 设 置 ");
        RelativeLayout.LayoutParams tt1 = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        int tt1Margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, this.getResources().getDisplayMetrics());
        tt1.setMargins(tt1Margins, tt1Margins, tt1Margins, tt1Margins);
        tt1.addRule(RelativeLayout.CENTER_IN_PARENT);
        titleTv.setLayoutParams(tt1);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        titleRv.addView(titleTv);

        //----------------------------下划线
        View view = new View(mContext);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        view.setBackgroundColor(Color.rgb(224, 224, 224));
        titleLinear.addView(view);

        return titleLinear;
    }

    private View getBody() {
        ScrollView scrollView = new ScrollView(mContext);
        scrollView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        StateListDrawable selector = new StateListDrawable();

        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));


        LinearLayout mainLinear = new LinearLayout(mContext);
        mainLinear.setOrientation(LinearLayout.VERTICAL);
        scrollView.addView(mainLinear);

        OptionItemView textSizeView = new OptionItemView(mContext);
        int heightInPx = (int)(45 * getResources().getDisplayMetrics().density);
        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, heightInPx);
        textSizeView.setLayoutParams(linearParams);
        textSizeView.setPadding(20, 20, 20, 20);
        textSizeView.setBackground(selector);
        textSizeView.setLeftImageMarginLeft(20);
        textSizeView.setLeftImageMarginRight(18);
        textSizeView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        textSizeView.setLeftText("菜单字体大小");
        textSizeView.setLeftTextSize(16);
        textSizeView.setLeftTextColor(Color.BLACK);
        textSizeView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        textSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuTextSize()));
        textSizeView.setOnClickListener(view -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入菜单字体大小");
            inputDialog.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
            inputDialog.getEditText().setText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuTextSize()));
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    int rowInt = Integer.parseInt(row);
                    SysConstConfig.getInstance().setFoodsMenuTextSize(rowInt);
                    inputDialog.dismiss();
                    textSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuTextSize()));

                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });
        mainLinear.addView(textSizeView);

        View line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView menuLinView = new OptionItemView(mContext);
        menuLinView.setPadding(20, 20, 20, 20);
        menuLinView.setLayoutParams(linearParams);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));
        menuLinView.setBackground(selector);
        menuLinView.setLeftImageMarginLeft(20);
        menuLinView.setLeftImageMarginRight(18);
        menuLinView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        menuLinView.setLeftText("菜单一行个数");
        menuLinView.setLeftTextSize(16);
        menuLinView.setLeftTextColor(Color.BLACK);
        menuLinView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        menuLinView.setRightText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuRow()));

        menuLinView.setOnClickListener(view -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入菜单一行个数");
            inputDialog.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
            inputDialog.getEditText().setText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuRow()));
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    int rowInt = Integer.parseInt(row);
                    SysConstConfig.getInstance().setFoodsMenuRow(rowInt);
                    inputDialog.dismiss();
                    menuLinView.setRightText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuRow()));

                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });
        mainLinear.addView(menuLinView);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView internalLineView = new OptionItemView(mContext);
        internalLineView.setPadding(20, 20, 20, 20);
        internalLineView.setLayoutParams(linearParams);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));
        internalLineView.setBackground(selector);
        internalLineView.setLeftImageMarginLeft(20);
        internalLineView.setLeftImageMarginRight(18);
        internalLineView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        internalLineView.setLeftText("菜单内部最大行数");
        internalLineView.setLeftTextSize(16);
        internalLineView.setLeftTextColor(Color.BLACK);
        internalLineView.setRightText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuInternalLine()));
        internalLineView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        internalLineView.setOnClickListener(view -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入菜单内部最大行数");
            inputDialog.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
            inputDialog.getEditText().setText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuInternalLine()));
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    int rowInt = Integer.parseInt(row);
                    SysConstConfig.getInstance().setFoodsMenuInternalLine(rowInt);
                    inputDialog.dismiss();
                    internalLineView.setRightText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuInternalLine()));

                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });
        mainLinear.addView(internalLineView);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView priceSizeView = new OptionItemView(mContext);
        priceSizeView.setPadding(20, 20, 20, 20);
        priceSizeView.setLayoutParams(linearParams);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));
        priceSizeView.setBackground(selector);
        priceSizeView.setLeftImageMarginLeft(20);
        priceSizeView.setLeftImageMarginRight(18);
        priceSizeView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        priceSizeView.setLeftText("菜单内部金额字体大小");
        priceSizeView.setLeftTextSize(16);
        priceSizeView.setLeftTextColor(Color.BLACK);
        priceSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuInternalPriceSize()));
        priceSizeView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        priceSizeView.setOnClickListener(view -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入菜单内部金额字体大小");
            inputDialog.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
            inputDialog.getEditText().setText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuInternalPriceSize()));
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    int rowInt = Integer.parseInt(row);
                    SysConstConfig.getInstance().setFoodsMenuInternalPriceSize(rowInt);
                    inputDialog.dismiss();
                    priceSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getFoodsMenuInternalPriceSize()));

                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });
        mainLinear.addView(priceSizeView);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView scanTakeSizeView = new OptionItemView(mContext);
        scanTakeSizeView.setPadding(20, 20, 20, 20);
        scanTakeSizeView.setLayoutParams(linearParams);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));
        scanTakeSizeView.setBackground(selector);
        scanTakeSizeView.setLeftImageMarginLeft(20);
        scanTakeSizeView.setLeftImageMarginRight(18);
        scanTakeSizeView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        scanTakeSizeView.setLeftText("扫码领餐列表信息字体大小");
        scanTakeSizeView.setLeftTextSize(16);
        scanTakeSizeView.setLeftTextColor(Color.BLACK);
        scanTakeSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getScanTakeFontSize()));
        scanTakeSizeView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        scanTakeSizeView.setOnClickListener(view -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入扫码领餐列表信息字体大小");
            inputDialog.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
            inputDialog.getEditText().setText(String.valueOf(SysConstConfig.getInstance().getScanTakeFontSize()));
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    int rowInt = Integer.parseInt(row);
                    SysConstConfig.getInstance().setScanTakeFontSize(rowInt);
                    inputDialog.dismiss();
                    scanTakeSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getScanTakeFontSize()));

                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });
        mainLinear.addView(scanTakeSizeView);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView shopCartSizeView = new OptionItemView(mContext);
        shopCartSizeView.setPadding(20, 20, 20, 20);
        shopCartSizeView.setLayoutParams(linearParams);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));
        shopCartSizeView.setBackground(selector);
        shopCartSizeView.setLeftImageMarginLeft(20);
        shopCartSizeView.setLeftImageMarginRight(18);
        shopCartSizeView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        shopCartSizeView.setLeftText("购物车信息字体大小");
        shopCartSizeView.setLeftTextSize(16);
        shopCartSizeView.setLeftTextColor(Color.BLACK);
        shopCartSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getShopCartFontSize()));
        shopCartSizeView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        shopCartSizeView.setOnClickListener(view -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入购物车信息字体大小");
            inputDialog.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
            inputDialog.getEditText().setText(String.valueOf(SysConstConfig.getInstance().getShopCartFontSize()));
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    int rowInt = Integer.parseInt(row);
                    SysConstConfig.getInstance().setShopCartFontSize(rowInt);
                    inputDialog.dismiss();
                    shopCartSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getShopCartFontSize()));
                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });
        mainLinear.addView(shopCartSizeView);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);
        return scrollView;
    }


    public static Bitmap drawableToBitmap(Drawable drawable) {
        // 取 drawable 的长宽
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();

        // 取 drawable 的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 建立对应 bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // 建立对应 bitmap 的画布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // 把 drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }


}
